package ui
{
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import ui.Tile;
	
	public class TileContainer extends Sprite
	{
		private var _tiles:Vector.<Tile>;
		private var _max_layers:int=0;
		private var _noFiles:TextField;
		
		public function TileContainer(width:Number=300, height:Number=300)
		{
			_tiles=new Vector.<Tile>();
			_noFiles=new TextField();
			_noFiles.defaultTextFormat=new TextFormat(Fonts.KozukaGothicProEL,50,0xffffff,null,null,null,null,null,TextFormatAlign.CENTER);
			_noFiles.embedFonts=true;
			_noFiles.selectable=false;
			_noFiles.width=width;
			_noFiles.height=height;
			_noFiles.y=height/3;
			_noFiles.text="No Files to Display";
			_noFiles.visible=false;
			addChild(_noFiles);
			displayWidth=width;
			displayHeight=height;
		}
		
		public function get layers():int
		{
			return _max_layers;
		}
		
		
		private var _zoom:int;
		public function get zoom():int
		{
			return _zoom;
		}
		public function set zoom(value:int):void
		{
			if(value==_zoom)
				return;
			updateZoom(value);
			
		}
		public function updateZoom(value:int):void{
			if(value>100)
				value=100;
			if(value<-100*(layers+0.4)&&layers)
				value=-100*(layers+0.4);
			_zoom = value;
			for each (var tile:Tile in _tiles){
				tile.k=_zoom/100;
			}
			positionTiles();
		}
		
		public function clear():void{
			for each (var tile:Tile in _tiles){
				removeChild(tile);
			}
			_max_layers=0;
			_tiles=new Vector.<Tile>();
			zoom=0;
		}
		
		public function positionTiles():void{
			sortDepth();
			for each (var tile:Tile in _tiles){
				tile.x=(tile.i+0.5)*_displayWidth/6;
				tile.y=(tile.j+0.5)*_displayHeight/3;
				tile.z=200*(tile.k+tile.layer);
				tile.alpha=getAlpha(tile.k+tile.layer);
				tile.visible=tile.alpha>0.1?true:false;
				tile.rotationY=(tile.i-2.5)*15;
			}
			if(_tiles.length>0)
				_noFiles.visible=false;
			else
				_noFiles.visible=true;
		}
		
		private function getAlpha(depth:Number):Number{
			if(depth>=-0.5&&depth<0)
				return (depth+0.5)*2;
			if(depth>0.75&&depth<=3)
				return -1/2.25*(depth-3);
			if(depth>=0&&depth<=0.75)
				return 1;
			return 0;
		}
		
		public function getZoom(zoom:Number):Number{
			if(zoom>100)
				return 100;
			if(zoom<(-100*(_max_layers)))
				return -100*(_max_layers);
			return zoom;
		}
		
		public function addTile(tile:Tile):void{
			_tiles.push(tile);
			addChild(tile);
			zoom=_zoom;
		}
		
		public function sortDepth():void{
			_max_layers=0;
			_tiles=_tiles.sort(function(x:Tile,y:Tile):Number{return y.layer-x.layer;});
			for(var t:int=0; t<_tiles.length; t++){
				_max_layers=(_tiles[t].layer>_max_layers)?_tiles[t].layer:_max_layers;
				setChildIndex(_tiles[t],t);
			}
		}
		
		private var _displayWidth:Number=0 ;
		public function get displayWidth():Number
		{
			return _displayWidth;
		}
		public function set displayWidth(value:Number):void
		{
			_displayWidth = value;
			_noFiles.width=value;
			positionTiles();
		}
		
		private var _displayHeight:Number=0;
		public function get displayHeight():Number
		{
			return _displayHeight;
		}
		public function set displayHeight(value:Number):void
		{
			_noFiles.height=value;
			_noFiles.y=value/3;
			_displayHeight = value;
			positionTiles();
		}
		
	}
}